<!-- component中的组件会帮我们自动进行注册 -->
<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue' //导入自封装的组件
import { artGetListService } from '@/api/article'
import { formatTime } from '@/utils/format.js' //时间规范化
import ArticleEdit from './components/ArticleEdit.vue' //导入抽屉

//预定义下 数据
const articleList = ref([]) //文章列表
const total = ref(0) //总条数
const loading = ref(false) //loading状态

// 添加按钮的逻辑
const articleEditRef = ref()
const onAddArticle = () => {
  articleEditRef.value.open({})
}
//编辑逻辑
const onEditArticle = (row) => {
  //console.log(row)
  articleEditRef.value.open(row)
}
//删除逻辑
const onDeleteArticle = (row) => {
  console.log(row)
}

//定义  获取-文章列表api 请求参数对象
const params = ref({
  pagenum: 1, //当前页
  pagesize: 5, //当前生效的每页条数
  cate_id: '',
  state: ''
})
//获取文章列表的方法
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList() //setup便执行

//处理分页逻辑
const onSizeChange = (size) => {
  //console.log('当前每页条数', size)
  //只要每条页数变化了，那么当前正在访问的当前页意义不大了
  //重新从第一页渲染即可
  params.value.pagenum = 1
  params.value.pagesize = size
  //基于最新的当前页 和 每页条数，渲染数据
  getArticleList()
}
const onCurrentChange = (page) => {
  //console.log('页码变化了', page)
  //更新当前页
  params.value.pagenum = page
  //基于最新的当前页 渲染数据
  getArticleList()
}

//搜索-按钮的逻辑==>按照最新的条件，重新检索
const onSearch = () => {
  params.value.pagenum = 1
  // select选择的条件，通过v-model绑定的数据cate_id
  getArticleList()
}
//重置-按钮的逻辑
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
//添加或编辑文章 成功的回调
const onSuccess = (type) => {
  if (type === 'add') {
    //如果是添加，最好渲染最好一页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
    getArticleList()
  } else {
    //如果是编辑，直接渲染当前页即可
    getArticleList()
  }
}
</script>

<template>
  <!-- 使用组件page-Container -->
  <!-- 默认插槽中传入内容
       具名插槽中传入一个el按钮 #extra选中插槽-->
  <page-Container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle"
        >添加文章</el-button
      ></template
    >

    <!-- 表单区域 3个item-->
    <el-form inline>
      <el-form-item label="文章分类">
        <!-- Vu3 v-model是:modelValue="cateId"和@update:modelValue的简写 
            目的是跟组件的数据双向绑定
        -->
        <ChannelSelect
          v-model="params.cate_id"
          style="width: 240px"
        ></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态">
        <!-- 这里后台标记发布状态，就是通过中文标记的 -->
        <el-select v-model="params.state" style="width: 240px">
          <el-option label="已发布" value="已发布"> </el-option>
          <el-option label="草稿" value="草稿"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -column柱-->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <!-- 组件el-table-column的default插槽，传来的每item=>row
        row是个对象 -->
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <!-- 两个按钮 -->
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- 抽屉 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
  </page-Container>
</template>

<style lang="scss" scoped></style>
